<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="展厅名称" prop="galleryName">
        <el-input v-model="queryParams.galleryName" placeholder="请输入展厅名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="主办方" prop="host">
        <el-input v-model="queryParams.host" placeholder="请输入主办方" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker v-model="queryParams.startDate" type="date" placeholder="请选择开始日期" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker v-model="queryParams.endDate" type="date" placeholder="请选择结束日期" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['gallery:gallery:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['gallery:gallery:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['gallery:gallery:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['gallery:gallery:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="galleryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="展厅名称" align="center" prop="galleryName" />
      <el-table-column label="主办方" align="center" prop="host" />
      <el-table-column label="开始日期" align="center" prop="startDate" width="120" />
      <el-table-column label="结束日期" align="center" prop="endDate" width="120" />
      <el-table-column label="展厅描述" align="center" prop="description" min-width="200">
        <template slot-scope="scope">
          <div class="description-cell">
            <div class="description-content">
              {{ parseDescription(scope.row.description) }}
            </div>
            <div v-if="scope.row.description && scope.row.description.length > 15" class="show-more" @click="showFullDescription(scope.row)">
              展示更多
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="海报图片" align="center" prop="poster" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.poster" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['gallery:gallery:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['gallery:gallery:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改展厅对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="small">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="展厅名称" prop="galleryName">
              <el-input v-model="form.galleryName" placeholder="请输入展厅名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主办方" prop="host">
              <el-input v-model="form.host" placeholder="请输入主办方" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="类型" prop="category">
              <el-select v-model="form.category" placeholder="请选择类型" style="width: 100%;">
                <el-option label="少儿" value="1"></el-option>
                <el-option label="艺术家" value="2"></el-option>
                <el-option label="大赛" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%;">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker v-model="form.startDate" type="date" placeholder="请选择开始日期" value-format="yyyy-MM-dd" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="endDate">
              <el-date-picker v-model="form.endDate" type="date" placeholder="请选择结束日期" value-format="yyyy-MM-dd" style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="展厅描述" prop="description">
              <el-input v-model="form.description" type="textarea" :rows="3" placeholder="请输入展厅描述" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="海报图片" prop="poster">
              <image-upload v-model="form.poster" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注信息" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    
    <!-- 完整描述对话框 -->
    <el-dialog :title="fullDescriptionDialog.title" :visible.sync="fullDescriptionDialog.visible" width="600px" append-to-body>
      <div class="full-description-content">{{ fullDescriptionDialog.content }}</div>
    </el-dialog>
  </div>
</template>

<script>
import { listGallery, getGallery, delGallery, addGallery, updateGallery } from "@/api/gallery/gallery"

export default {
  name: "Gallery",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 展厅表格数据
      galleryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        galleryName: null,
        host: null,
        startDate: null,
        endDate: null,
        description: null,
        poster: null,
        status: null,
        category: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        galleryName: [
          { required: true, message: "展厅名称不能为空", trigger: "blur" }
        ],
      },
      // 添加控制描述显示的变量
      fullDescriptionDialog: {
        visible: false,
        content: '',
        title: '展厅描述详情'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询展厅列表 */
    getList() {
      this.loading = true
      listGallery(this.queryParams).then(response => {
        this.galleryList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        galleryId: null,
        galleryName: null,
        host: null,
        startDate: null,
        endDate: null,
        description: null,
        poster: null,
        status: null,
        category: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.galleryId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加展厅"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const galleryId = row.galleryId || this.ids
      getGallery(galleryId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改展厅"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.galleryId != null) {
            updateGallery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addGallery(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const galleryIds = row.galleryId || this.ids
      this.$modal.confirm('是否确认删除展厅编号为"' + galleryIds + '"的数据项？').then(function () {
        return delGallery(galleryIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 解析描述内容，按规范截取 */
    parseDescription(description) {
      if (!description) return '';
      // 按规范处理长度：不超过15位直接显示；超过15位则显示前10位和后5位，中间用省略号连接
      if (description.length <= 15) return description;
      return description.substring(0, 10) + '...' + description.substring(description.length - 5);
    },
    
    /** 显示完整描述 */
    showFullDescription(row) {
      this.fullDescriptionDialog.content = row.description;
      this.fullDescriptionDialog.visible = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gallery/gallery/export', {
        ...this.queryParams
      }, `gallery_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  text-align: right;
  padding: 15px 20px;
  border-top: 1px solid #e8eaec;
}

.description-cell {
  text-align: left;
}

.description-content {
  line-height: 1.5;
}

.show-more {
  color: #409EFF;
  cursor: pointer;
  margin-top: 5px;
  display: inline-block;
}

.full-description-content {
  text-align: left;
  line-height: 1.6;
  white-space: pre-wrap;
}

</style>
